package com.scuec.groupfive.controller;

import com.scuec.groupfive.entity.SysLog;
import com.scuec.groupfive.mapper.SysLogMapper;
import com.scuec.groupfive.service.LogService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/log")
@CrossOrigin(maxAge = 3600)
public class LogController {

    @Resource
    private SysLogMapper sysLogMapper;

    @Resource
    private LogService logService; // 新增LogService注入

    @GetMapping("/list")
    public Map<String, Object> getLogList() {
        Map<String, Object> result = new HashMap<>();
        List<SysLog> logs = sysLogMapper.selectAllLogs();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", logs);
        return result;
    }
    // http://localhost:8080/api/log/listByCondition
    @GetMapping("/listByCondition")
    public Map<String, Object> getLogListByCondition(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        System.out.println("Received query - username: " + username +
                ", startDate: " + startDate +
                ", endDate: " + endDate +
                ", pageNum: " + pageNum +
                ", pageSize: " + pageSize);

        Map<String, Object> result = new HashMap<>();
        try {
            int offset = (pageNum - 1) * pageSize;
            List<SysLog> logs = sysLogMapper.selectLogsByCondition(username, startDate, endDate, offset, pageSize);
            int total = sysLogMapper.countLogsByCondition(username, startDate, endDate);

            System.out.println("Query result - count: " + (logs != null ? logs.size() : 0) +
                    ", total: " + total);

            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", logs);
            result.put("total", total);
        } catch (Exception e) {
            System.err.println("Query failed: " + e.getMessage());
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "查询失败: " + e.getMessage());
        }
        return result;
    }
    // http://localhost:8080/api/log/countLogins
    // 新增用户登录次数统计接口
    @GetMapping("/countLogins")
    public Map<String, Object> countUserLogins(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {

        Map<String, Object> result = new HashMap<>();
        try {
            List<Map<String, Object>> loginStats = logService.countUserLogins(startDate, endDate);
            result.put("code", 200);
            result.put("msg", "统计成功");
            result.put("data", loginStats);
        } catch (Exception e) {
            System.err.println("统计失败: " + e.getMessage());
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "统计失败: " + e.getMessage());
        }
        return result;
    }
}